package cn.itcast.goods.user.dao;

import cn.itcast.goods.user.DBConn.DbConn;
import cn.itcast.goods.user.domain.User;
import cn.itcast.jdbc.TxQueryRunner;
import com.mysql.jdbc.Connection;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

public class UserDao {
    QueryRunner queryRunner=new TxQueryRunner();
    Connection connection;
    /**
     * 校验用户名是否存在
     * @param name
     * @return
     */
    public boolean AJAXcheckUserName(String name)
    {
        String sql="select count(1) from t_user where loginname=?";
        String[] param={name};
        Number number=null;
         connection= DbConn.getConnection();
        try{

            number=(Number) queryRunner.query(connection,sql,new ScalarHandler(),param);
        }
        catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
        return number.intValue()==0;
    }

    /**
     * 校验用户邮箱是否存在
     * @param name
     * @return
     */
    public boolean AJAXcheckEmail(String name)
    {
        String sql="select count(1) from t_user where email=?";
        connection= DbConn.getConnection();
        String[] param={name};
        Number number=null;
        try{
            number=(Number) queryRunner.query(connection,sql,new ScalarHandler(),param);
        }
        catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
        return number.intValue()==0;
    }
    public void  addUser(User user)
    {

        String sql="insert into t_user values (?,?,?,?,?,?)";
        Object[] param={user.getUid(),user.getLoginname(),user.getLoginpass(),user.getEmail(),user.getStatus(),user.getActivationCode()};
        try{
           queryRunner.update(connection,sql,param);
        }
        catch (SQLException e)
        {
            throw new RuntimeException(e);
        }

    }
}
